package com.promis.widget.mediapicker.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.promis.widget.R;
import com.promis.widget.listener.OnAvoidMultiClickListener;
import com.promis.widget.mediapicker.data.DataCallback;
import com.promis.widget.mediapicker.data.VideoLoader;
import com.promis.widget.mediapicker.entity.FolderParcelable;
import com.promis.widget.mediapicker.entity.MediaParcelable;
import com.promis.widget.mediapicker.entity.VideoParcelable;
import com.promis.widget.mediapicker.manager.MediaPickerConstants;
import com.promis.widget.mediapicker.manager.ImagePickerManager;
import com.promis.widget.mediapicker.manager.VideoPickerManager;
import com.promis.widget.mediapicker.util.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jidanbo on 2018/3/26.
 */

public class VideoPickerActivity extends AppCompatActivity implements DataCallback {

    private ImageView imgvClose;
    private TextView txtvTitle;

    private Button btnvSubmit;
    private Button btnvFolder;
    private Button btnvPreview;

    private RecyclerView rylvMedia;
    private VideoGridAdapter adptMedia;

    private ListPopupWindow mFolderPopupWindow;
    private FolderAdapter mFolderAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mediapicker_picker);

        imgvClose = findViewById(R.id.activity_media_picker_imgv_close);
        txtvTitle = findViewById(R.id.activity_media_picker_txtv_title);

        btnvSubmit = findViewById(R.id.activity_media_picker_btnv_submit);
        btnvFolder = findViewById(R.id.activity_media_picker_btnv_folder);
        btnvPreview = findViewById(R.id.activity_media_picker_btnv_preview);

        rylvMedia = findViewById(R.id.activity_media_picker_rylv_grid);

        initData();
        initListener();
    }

    private void initData() {
        getLoaderManager().initLoader(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO,null,new VideoLoader(this,this));

        GridLayoutManager mLayoutManager = new GridLayoutManager(this, MediaPickerConstants.GridSpanCount);
        rylvMedia.setLayoutManager(mLayoutManager);
        rylvMedia.addItemDecoration(new SpacingDecoration(MediaPickerConstants.GridSpanCount, MediaPickerConstants.GridSpace));
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rylvMedia.setHasFixedSize(true);
        adptMedia = new VideoGridAdapter(this, VideoPickerManager.getInstance().getMaxDuration(), VideoPickerManager.getInstance().getMaxFileSize());
        rylvMedia.setAdapter(adptMedia);

        ArrayList<FolderParcelable> folders=new ArrayList<>();
        mFolderAdapter=new FolderAdapter(folders,this);
        mFolderPopupWindow = new ListPopupWindow(this);
        mFolderPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        mFolderPopupWindow.setAdapter(mFolderAdapter);
        mFolderPopupWindow.setHeight((int)(ScreenUtils.getScreenHeight(this)*0.6));
        mFolderPopupWindow.setWidth(ScreenUtils.getScreenWidth(this));
        mFolderPopupWindow.setAnchorView(btnvFolder);
        mFolderPopupWindow.setModal(true);
        mFolderPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mFolderAdapter.setSelectIndex(position);
                btnvFolder.setText(mFolderAdapter.getItem(position).name);

                List<VideoParcelable> videoList = new ArrayList<>();
                for (MediaParcelable media : mFolderAdapter.getSelectMedias()) {
                    if (media instanceof VideoParcelable) {
                        VideoParcelable video = (VideoParcelable) media;
                        if (video.duration > 0 && video.size > 0) {
                            videoList.add(video);
                        }
                    }
                }

                adptMedia.notifyDataSetChanged(videoList);
                mFolderPopupWindow.dismiss();
            }
        });
        btnvPreview.setVisibility(View.GONE);
        btnvSubmit.setVisibility(View.GONE);
    }

    private void initListener() {
        imgvClose.setOnClickListener(new OnAvoidMultiClickListener() {
            @Override
            public void onAvoidMultiClick(View v) {
                finish();
            }
        });

        adptMedia.setOnItemClickListener(new VideoGridAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, VideoParcelable video) {
                setButtonText();
                done(video);
            }
        });

        btnvFolder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mFolderPopupWindow.isShowing()){
                    mFolderPopupWindow.dismiss();
                }else{
                    mFolderPopupWindow.show();
                }
            }
        });

        btnvFolder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getBaseContext(), VideoPickerActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onData(ArrayList<FolderParcelable> list) {
        ArrayList<MediaParcelable> mediaList = list.get(0).getMedias();

        List<VideoParcelable> videoList = new ArrayList<>();
        for (MediaParcelable media : mediaList) {
            if (media instanceof VideoParcelable) {
                VideoParcelable video = (VideoParcelable) media;
                if (video.duration > 0 && video.size > 0) {
                    videoList.add(video);
                }
            }
        }

        adptMedia.notifyDataSetChanged(videoList);
        setButtonText();
    }

    private void setButtonText(){
//        btnvSubmit.setText(getString(R.string.done)+"("+adptMedia.getSelectedMediaList().size()+"/"+ VideoPickerManager.getInstance().getMaxFileCount()+")");
//        btnvPreview.setText(getString(R.string.preview)+"("+adptMedia.getSelectedMediaList().size()+")");
    }

    public void done(VideoParcelable selectedVideo){
        if (VideoPickerManager.getInstance().getVideoPickerListener() != null) {
            VideoPickerManager.getInstance().getVideoPickerListener().onPickCompleted(selectedVideo);
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        Glide.get(this).clearMemory();
        super.onDestroy();
    }
}
